﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Baza.EF.BLL;
using Baza.EF.DAL;

namespace Pep_3014_3070.Anonymous
{
    public partial class NewUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindeCountry();
            }
        }

        private void BindeCountry()
        {
            List<Countries> temp = DARegistration.getAllCountres();
            Countries c = new Countries();
            c.Country_name = "Odaberi zemlju";
            c.CountryID = 0;
            temp.Insert(0, c);

            CountryDropDownList.DataSource = temp;
            CountryDropDownList.DataTextField = "Country_name";
            CountryDropDownList.DataValueField = "CountryID";
            CountryDropDownList.DataBind();
        }

        protected void RegisterButton_Click(object sender, EventArgs e)
        {
            if (FirstNameInput.Text != "" && LastNameInput.Text != "" && EmailInput.Text != "" && PasswordInput.Text != "" && ConfirmPasswordInput.Text != ""
                && Convert.ToInt32(CountryDropDownList.SelectedIndex) != 0 && Address1Input.Text != "" && Address2Input.Text != ""
                && CityInput.Text != "" && StreetNumberInput.Text != ""
                )
            {
                Baza.EF.DAL.Users u = new Baza.EF.DAL.Users();
                u.FirstName = FirstNameInput.Text;
                u.LastName = LastNameInput.Text;
                u.Email = EmailInput.Text;
                u.PasswordSalt = UIHelper.GenerateSalt();
                u.PasswordHash = UIHelper.GenerateHash(PasswordInput.Text, u.PasswordSalt);
                u.TypeID = 3;
                u.Status = true;
                u.Phone = PhoneInput.Text;

                Address ad = new Address();
                ad.Address1 = Address1Input.Text;
                ad.Address2 = Address2Input.Text;
                ad.StreetNumber = Convert.ToInt32(StreetNumberInput.Text);


                Cities cit = new Cities();
                cit.Name = CityInput.Text;
                cit.CountryID = Convert.ToInt32(CountryDropDownList.SelectedIndex);

                DARegistration.RegisterUsers(u, ad, cit);
                //NAPRAVITI REDIRECT NA PROFILE EDIT PAGE  . . .

                clearArea();

            }
        }

        private void clearArea()
        {
            FirstNameInput.Text = "";
            LastNameInput.Text = "";
            EmailInput.Text = "";
            StreetNumberInput.Text = "";
            Address1Input.Text = "";
            Address2Input.Text = "";
            CityInput.Text = "";
            PhoneInput.Text = "";
            CountryDropDownList.SelectedIndex = 0;

        }
    }
}